package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.ProductService;
import com.hh.until.Tool;
import com.hh.vo.ProductVO;
import lombok.experimental.Tolerate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/findAll")
    public ResultDTO findAll(ProductVO productVO) {
        productVO.setPriceStart(Tool.strToDecimal(productVO.getPriceStart()));
        productVO.setPriceEnd(Tool.strToDecimal(productVO.getPriceEnd()));
        productVO.setName(Tool.nullToStr(productVO.getName()));
        productVO.setTypeId(Tool.nullToInt(productVO.getTypeId()));
        productVO.setEnable(Tool.nullToInt(productVO.getEnable()));
        productVO.setPage(Tool.strToPage(productVO.getPage().toString()));
        productVO.setLimit(Tool.strToLimit(productVO.getLimit().toString()));

        ResultDTO resultDTO = productService.findAll(productVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(ProductVO productVO) {
        productVO.setId(Tool.strToInt(productVO.getId().toString()));

        ResultDTO resultDTO = productService.findById(productVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(ProductVO productVO) {
        productVO.setPrice(Tool.strToDecimal(productVO.getPrice()));
        productVO.setName(Tool.nullToStr(productVO.getName()));
        productVO.setTypeId(Tool.strToInt(productVO.getTypeId().toString()));
        productVO.setEnable(Tool.nullToInt(productVO.getEnable()));
        productVO.setImg(Tool.nullToStr(productVO.getImg()));
        productVO.setRemark(Tool.nullToStr(productVO.getRemark()));

        ResultDTO resultDTO = productService.add(productVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(ProductVO productVO) {
        productVO.setId(Tool.strToInt(productVO.getId().toString()));

        ResultDTO resultDTO = productService.delete(productVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(ProductVO productVO) {
        productVO.setId(Tool.strToInt(productVO.getId().toString()));
        productVO.setPrice(Tool.strToDecimal(productVO.getPrice()));
        productVO.setName(Tool.nullToStr(productVO.getName()));
        productVO.setTypeId(Tool.strToInt(productVO.getTypeId().toString()));
        productVO.setEnable(Tool.nullToInt(productVO.getEnable()));
        productVO.setImg(Tool.nullToStr(productVO.getImg()));
        productVO.setRemark(Tool.nullToStr(productVO.getRemark()));

        ResultDTO resultDTO = productService.update(productVO);
        return resultDTO;
    }

    @GetMapping("/count")
    public ResultDTO count(ProductVO productVO) {
        productVO.setPriceStart(Tool.strToDecimal(productVO.getPriceStart()));
        productVO.setPriceEnd(Tool.strToDecimal(productVO.getPriceEnd()));
        productVO.setTypeId(Tool.nullToInt(productVO.getTypeId()));
        productVO.setEnable(Tool.nullToInt(productVO.getEnable()));
        productVO.setName(Tool.nullToStr(productVO.getName()));
        productVO.setEnable(Tool.nullToInt(productVO.getEnable()));

        ResultDTO resultDTO = productService.count(productVO);
        return resultDTO;
    }
}
